<template>
  <div class="container">
    <header>
      <div class="title">用户注册</div>
    </header>

    <ul class="userform">
      <li>
        <div class="formleft">手机号码:</div>
        <div class="formright">
          <input type="text" placeholder="手机号码" v-model.trim="userId" @blur="checkMobile" />
        </div>
      </li>
      <li>
        <div class="formleft">密码:</div>
        <div class="formright">
          <input type="password" placeholder="密码" v-model.trim="password" />
        </div>
      </li>
      <li>
        <div class="formleft">确认密码:</div>
        <div class="formright">
          <input type="password" placeholder="确认密码" v-model.trim="confirmpassword" />
        </div>
      </li>
      <li>
        <div class="formleft">用户姓名:</div>
        <div class="formright">
          <input type="text" placeholder="用户姓名" v-model.trim="userName" />
        </div>
      </li>
      <li>
        <div class="formleft">性别:</div>
        <div class="formright" style="font-size: 3vw;">
          <input
            type="radio"
            name="sex"
            style="width: 6vw;height: 3.2vw;"
            value="1"
            v-model="userSex"
          /> 男
          <input
            type="radio"
            name="sex"
            style="width: 6vw;height: 3.2vw;"
            value="0"
            v-model="userSex"
          /> 女
        </div>
      </li>
    </ul>

    <div class="register-btn" @click="saveUser">
      <button>注册</button>
    </div>

    <Footer></Footer>
  </div>
</template>

<script>
import Footer from "../components/Footer";
export default {
  name: "Register",
  data() {
    return {
      userId: "",
      password: "",
      userName: "",
      userSex: 1,
      confirmpassword: "",
      checkMobileFlag:false
    };
  },
  components: {
    Footer
  },
  methods: {
    saveUser() {
        this.checkMobile();
      if (
        this.userId != "" &&
        this.password != "" &&
        this.confirmpassword != "" &&
        this.userName != "" &&
        this.checkMobileFlag
      ) {
        this.$axios
          .post(
            "/UserController/saveUser",
            this.$qs.stringify({
              password: this.password,
              userId: this.userId,
              userName: this.userName,
              userSex: this.userSex
            })
          )
          .then(response => {
            if (response.data == 0) {
              alert("用户注册失败");
            } else {
              this.$router.go(-1);
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    },
    checkMobile() {
      this.$axios
        .post(
          "/UserController/getUserById",
          this.$qs.stringify({
            userId: this.userId
          })
        )
        .then(response => {
          if (response.data != 0) {
            alert("用户手机号重复");
            this.checkMobileFlag = false
          } else{
              this.checkMobileFlag = true
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped>
/*********************header部分**********************/
.container header {
  width: 100%;
  height: 12vw;
}
.container header .title {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  color: #fff;
  font-size: 4.8vw;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
}
/*********************输入框部分**********************/
.container .userform li {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  align-items: center;
}
.container .userform li .formleft {
  color: #666;
  font-weight: bold;
  flex: 0 0 18vw;
  font-size: 3vw;
}
.container .userform li .formright {
  flex: 1;
}
.container .userform li input {
  border: none;
  outline: none;
  font-size: 3vw;
  height: 4vw;
  width: 100%;
}
/*********************按钮部分**********************/
.container .register-btn {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}
.container .register-btn button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: bold;
  color: white;
  background-color: #38ca73;
  border: none;
  outline: none;
  border-radius: 4px;
}
</style>